<?php

namespace app\admin\controller;

class SettingBase
{
    public function index()
    {
        $query = \app\model\Setting::field(['id', 'group_code', 'type', 'code', 'value', 'label']);

        $models = $query->select();

        return json(['code' => 20000, 'data' => ['settings' => $models]]);
    }

    public function delete()
    {
        $model = \app\model\Setting::destroy(input('id'));

        return json(['code' => 20000, 'msg' => '移除成功', 'data' => $model]);
    }

    public function update()
    {
        $model = \app\model\Setting::update(input());

        if ($model->id) {
            $model = \app\model\Setting::find($model->id);
        }

        return json(['code' => 20000, 'msg' => '更新成功', 'data' => $model]);
    }

    public function create()
    {
        $param = request()->param();

        $model = \app\model\Setting::create($param);

        if (!$model) {
            return json(['code' => 40400, 'msg' => '新增失败']);
        }

        return json(['code' => 20000, 'msg' => '新增成功', 'data' => $model]);
    }

    public function detail()
    {
        $query = \app\model\Setting::field(['id', 'group_code', 'type', 'code', 'value', 'label']);

        if ($id = input('id')) {
            $query->where('id', $id);
        }

        $model = $query->find();

        return json(['code' => 20000, 'data' => ['setting' => $model]]);
    }
}
